﻿using Models;
using SIE.SCADA.SqlSugar;
using SIE.SCADA.ZemtDemo.Common;
using SIE.SCADA.ZemtDemo.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SIE.SCADA.ZemtDemo.View
{
    /// <summary>
    /// EquipmentView.xaml 的交互逻辑
    /// </summary>
    public partial class LoginView : UserControl
    {
        
        private ZemtDemoServer zemtDemoServer;

        public LoginView()
        {
            InitializeComponent();

        }

        public LoginView(ZemtDemoServer zemtDemoServer)
        {
            InitializeComponent();
            this.zemtDemoServer = zemtDemoServer; 
        }
       

        private void Login_Click(object sender, RoutedEventArgs e)
        {






            string username = "";
            string password = "";
            //if (UserName.Text !="" && Password.Password !="") { 
                username = UserName.Text;
                password = Password.Password;

                Password.Clear();

            //}
            


            PgSqlAccess pgsql = new PgSqlAccess();
            List<Sys_User_UserRole> Records = new List<Sys_User_UserRole>();
            using (var ctx = pgsql.GetClient())
            {

                if (pgsql.DBClient is SmdcOrmClient ormClient)
                {
                    string sql = " 1=1 ";
                    if (username != null && username != "")
                    {
                        sql += " and userid_str = '" + username + "'";
                    }
                    else {
                        MessageBox.Show("请输入：用户名");
                        return;
                    }
                    if (password != null && password != "")
                    {
                        sql += " and remark_str =  '" + password + "'";
                    }
                    else {
                        MessageBox.Show("请输入：密码");
                        return;
                    }

                    Records = ormClient.SqlSugarClient.Queryable<Sys_User_UserRole>().Where(sql).ToList();
                }
            }

            if (Records != null && Records.Count > 0)
            {
                if (!String.IsNullOrEmpty(Records[0].RoleID_Str)) {
                    zemtDemoServer.InitTab(Records[0].RoleID_Str);
                }
                else
                {
                    MessageBox.Show("该用户权限为空");
                    return;
                }
            }
            else {
                MessageBox.Show("用户名或密码错误");
                return;


            }





            
        }

        private void Logout_Click(object sender, RoutedEventArgs e)
        {

        }
    }
}
